package com.duang.framework.dao.plugin;

import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.logging.Logger;

import javax.sql.DataSource;

import com.duang.framework.dao.AbstractConnectionPool;
import com.duang.framework.dao.DataSourceProvider;
import com.duang.framework.dao.Plugin;

public class DefaultPlugin extends AbstractConnectionPool implements
		DataSourceProvider, Plugin {
	private DataSource dataSource;

	public DefaultPlugin(String jdbcUrl, String user, String password,
			String driverClass) {
		super(jdbcUrl, user, password, driverClass);

	}

	public void init() {
		try {
			Class.forName(driverClass);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}

	@Override
	public boolean start() {
		return true;
	}

	@Override
	public boolean stop() {
		return true;
	}

	@Override
	public DataSource getDataSource() {
		if (dataSource == null) {
			dataSource = new DataSourceImpl();
		}
		return dataSource;
	}

	public class DataSourceImpl implements DataSource {

		@Override
		public PrintWriter getLogWriter() throws SQLException {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public void setLogWriter(PrintWriter out) throws SQLException {
			// TODO Auto-generated method stub

		}

		@Override
		public void setLoginTimeout(int seconds) throws SQLException {
			// TODO Auto-generated method stub

		}

		@Override
		public int getLoginTimeout() throws SQLException {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public Logger getParentLogger() throws SQLFeatureNotSupportedException {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public <T> T unwrap(Class<T> iface) throws SQLException {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public boolean isWrapperFor(Class<?> iface) throws SQLException {
			// TODO Auto-generated method stub
			return false;
		}

		@Override
		public Connection getConnection() throws SQLException {
			return DriverManager.getConnection(jdbcUrl, user, password);
		}

		@Override
		public Connection getConnection(String username, String password)
				throws SQLException {
			return DriverManager.getConnection(jdbcUrl, username, password);
		}

	}

}
